﻿using System;
using System.Web.UI.WebControls;
using CustomerContactSystem.Core.Model;

namespace CustomerContactSystem.WebForms.Admin.Sources {
    public partial class List : BasePage {
        protected void Page_Load(object sender, EventArgs e) {
            if (Page.IsPostBack) {
                return;
            }

            GetSources();
        }

        private void GetSources() {
            var sources = EnquirySourceRepository.QueryOver().OrderBy(s => s.SourceName).Asc.List();
            SourcesList.DataSource = sources;
            SourcesList.DataBind();
        }

        protected void SourcesListItemDataBound(object sender, RepeaterItemEventArgs e) {
            if (e.Item.ItemType != ListItemType.Item && e.Item.ItemType != ListItemType.AlternatingItem) {
                return;
            }

            var source = (EnquirySource)e.Item.DataItem;
            var editLink = (HyperLink) e.Item.FindControl("EditLink");
            editLink.NavigateUrl = "~/Admin/Sources/Edit.aspx?id=" + source.Id;
        }
    }
}